@include('vendor.ueditor.assets')
<meta name="csrf-token" content="{{ csrf_token() }}">
<main id="add_commodity" class="add_commodity">
    <nav id="add_commodity_nav" class="add_commodity_nav">
        <ul class="nav nav-pills ">
            <li role="presentation"><a href="{{asset($nav['one']['url'])}}">{{$nav['one']['name']}}</a></li>
            <li role="presentation" class="active"><a href="{{asset($nav['two']['url'])}}">{{$nav['two']['name']}}</a>
            </li>
            <li role="presentation"><a href="{{asset($nav['three']['url'])}}">{{$nav['three']['name']}}</a></li>
        </ul>
    </nav>
    <div class="form-horizontal" style="padding: 2vh 0 0 0;">
        <div class="form-group">
            <label for="inputNmae" class="col-sm-1 control-label">品名:</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" id="inputNmae" placeholder="品名">
            </div>
        </div>
        <div class="form-group">
            <label for="inputTitle" class="col-sm-1 control-label">标题</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="inputTitle" placeholder="标题">
            </div>
        </div>
        <div class="form-group">
            <label for="inputClassify" class="col-sm-1 control-label">分类</label>
            <div class="col-sm-10">
                @foreach($classify as $key => $value)
                    <div class="parent-classify-box" parent-id="$key" check="0">
                        <labe class="control-label checkbox-inline parent-classify" style="color: #ba3b3f">
                            {{$value['classify_name']}} :
                        </labe>
                        @foreach($value['data'] as $k => $v)
                            <label class="checkbox-inline children-classify">
                                <input type="checkbox" id="children{{$k}}" value="{{$k}}" ><span>{{$v["classify_name"]}}</span>
                            </label>
                        @endforeach
                    </div>
                @endforeach
            </div>
        </div>
        <div class="form-group">
            <label for="inputImg" class="col-sm-1 control-label">首页图片</label>
            <div class="col-sm-10">
                @include('Upload.asset')
            </div>
        </div>
        <div class="form-group">
            <label for="inputText4" class="col-sm-1 control-label">商品说明</label>
            <div class="col-sm-10">
                <script id="container" name="content" type="text/plain"></script>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-1 col-sm-10">
                <button class="btn btn-default" id="save">保存</button>
            </div>
        </div>
    </div>
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        // window.location.reload()
        var ue = UE.getEditor('container');
        ue.ready(function () {
            ue.execCommand('serverparam', '_token', '{{ csrf_token() }}'); // 设置 CSRF token.
        });
    </script>
    <script>
        var check_data = {}
        check_data['length'] = 0
        $('input[type="checkbox"]').click(function () {
            return false
        })
        $(".children-classify").click(function () {
            let input = $(this).children('input')
            let is = input.is(":checked")
            let span = $(this).children('span')
            let parent = $(this).parents('.parent-classify-box')
            console.log(is)
            if(is){
                console.log('本行可再次选中')
                parent.attr('check',0)
                input.prop("checked",false)
                delete check_data[input.val()]
                check_data.length--
            }else {
                if(check_data.length >= 3){
                    alert('不能大于三个分类')
                    return false
                }
                if(parent.attr('check') == 1){
                    input.prop("checked",false)
                }else{
                    parent.attr('check',1)
                    input.prop("checked",true)
                    check_data[input.val()] = span.text()
                    check_data.length++
                }
            }
            return false
        })
        // uploader.up_img_url = 'http://shop-1253123347.file.myqcloud.com/uploads/2018-03-11/img/2018-03-11-10-37-05-5aa496513a472.jpg'
        $('#save').click(function () {
            delete check_data.length
            console.log('111')
            var data = {
                _token: $('meta[name="csrf-token"]').attr('content'),
                name: $('#inputNmae').val(),
                title: $('#inputTitle').val(),
                classify_ids : check_data,
                url: uploader.up_img_url,
                content: ue.getContent()
            }
            $.post('/admin/commodity/api_commodity/add_commodity',data, function(data) {
                if(data.errorCode == 0){
                    window.location.reload()
                    // window.location.href = '/admin/commodity/'
                    return false
                }
                else
                    alert(data.errorMsg)
            },'json')
        })
    </script>
</main>